//
//  UIButton+EZR_Extension.m
//  test
//
//  Created by MacDev02 on 2019/1/22.
//  Copyright © 2019年 www.schinta.com.cn. All rights reserved.
//

#import "UIButton+EZR_Extension.h"

@implementation UIButton (EZR_Extension)
- (EZRMutableNode<NSString *> *)ezr_textNode {
    EZRMutableNode<NSString *> *node = objc_getAssociatedObject(self, @selector(ezr_textNode));
    if (node == nil) {
        node = [self generatorTextNode];
        objc_setAssociatedObject(self, @selector(ezr_textNode), node, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
    }
    return node;
}

- (EZRMutableNode<NSString *> *)generatorTextNode {
    EZRMutableNode<NSString *> *node = [EZRMutableNode new];
    @ezr_weakify(self, node)
    [[node listenedBy:self.titleLabel] withContextBlock:^(NSString * _Nullable next, id  _Nullable context) {
        @ezr_strongify(self)
        if (![context isEqualToString:[self uniqueContext]]) {
            [self setTitle:next forState:UIControlStateNormal];
        }
    }];
    [[EZR_PATH(self, titleLabel.text) listenedBy:self] withBlock:^(id  _Nullable next) {
        @ezr_strongify(self, node)
        [node setValue:next context:[self uniqueContext]];
    }];
    return node;
}

- (NSString *)uniqueContext {
    return [NSString stringWithFormat:@"ezr_updateText_%p", self];
}
@end
